﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Base.Designer;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Designer.BRules;
using OpenComposite.EII.Designer.Generic;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCRepositoryBusinessRuleUI : RepositoryItemUI
	{
		#region Initialization
		public UCRepositoryBusinessRuleUI(IServiceProvider serviceprovider)
			: base(serviceprovider)
		{
			InitializeComponent();
			init();
		}

		private void init()
		{
			_designerViewControl = new DesignerViewControl();
			_designerViewControl.Dock = DockStyle.Fill;
			RepositorySurfaceManager surfacemanager = serviceProvider as RepositorySurfaceManager;
			_designerViewControl.DesignController = new BRulesDesignController(surfacemanager);
			_designerViewControl.Types = new Type[] { typeof(BusinessRuleSymbol), typeof(IFSymbol), typeof(THENResultSymbol), typeof(ELSEResultSymbol), typeof(ONERRORResultSymbol), typeof(ORSymbol), typeof(ANDSymbol), typeof(XORSymbol), typeof(Route) };
			tabBRule.Controls.Add(_designerViewControl);
		}
		#endregion

		#region Properties
		public override OpenComposite.EII.Repository.IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _brule;
			}
			set
			{
				if ( value is RepositoryBusinessRule ) {
					_brule = (RepositoryBusinessRule)value;
					load();
				}
			}
		}

		private BRulesDesignController BRuleDesignController
		{
			get
			{
				if ( _designerViewControl == null )
					throw new NullReferenceException("DesignerViewControl is null");
				return _designerViewControl.DesignController as BRulesDesignController;
			}
		}

		public override string TypeText
		{
			get
			{
				return Resources.sBRule;
			}
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#region Methods

		private void load()
		{
			if ( _brule == null )
				return;

			txtName.Text = _brule.Name;
			txtDesc.Text = _brule.Description;

			try {
				RepositoryItemDesigner rid = RepositoryService.GetRepositoryItemDesigner(this.RepositoryItem);
				this.BRuleDesignController.Load(rid);

				_designerViewControl.DesignerControls.Clear();

				_designerViewControl.ReadOnly = true;

				RepositoryService repSvc = serviceProvider.GetService(typeof(RepositoryService)) as RepositoryService;

				if ( _brule.Diagram == null ) {
					MessageBox.Show("Please open and save the Businesss Policy containing the Business Rule to display and try again.",
						"Missing information", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}
				//Point newLocation = this.DesignerView.PointToClient(new Point(e.X, e.Y));
				BRulesDesignController controller = ( (BRulesDesignController)_designerViewControl.DesignController );
				BusinessRuleSymbol brule = controller.LoadBusinessRuleFromXmlDiagram(this.RepositoryItem as RepositoryBusinessRule, Point.Empty, _types);
				brule.Location = new Point(10, 10);
				_designerViewControl.DesignerControls.Add(brule);
				_designerViewControl.MainPanel.Location = new Point(0, 0);
				controller.DesignerPanel.Location = new Point(0, 0);
				_designerViewControl.SetVScrollbarValue(0);
				_designerViewControl.SetHScrollbarValue(0);
				_designerViewControl.MainPanel.Refresh();
				_designerViewControl.MainPanel.Update();

				//brule.Show();
				//BusinessRuleSymbol symbol = ((BRulesDesignController)_designerViewControl.DesignController).LoadBusinessRuleFromXmlDiagram(_brule, Point.Empty);
				//_designerViewControl.AddDesignerControl(symbol);
			} catch {
				throw;
			}

			if ( tabControl1.SelectedTab == tabUsedBy ) {
				usedByHierarchyBRule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( tabControl1.SelectedTab == tabUses ) {
				usingHierarchyBRule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}

		}

		private void save()
		{
			if ( _brule == null )
				_brule = new RepositoryBusinessRule();

			_brule.Reload();

			_brule.Name = txtName.Text;
			_brule.Description = txtDesc.Text;
		}

		private void tabControl1_Selected(object sender, TabControlEventArgs e)
		{
			if ( e.TabPage == tabUsedBy ) {
				usedByHierarchyBRule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( e.TabPage == tabUses ) {
				usingHierarchyBRule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}
		}

		#endregion

		#region Variables
		private RepositoryBusinessRule _brule = null;
		private DesignerViewControl _designerViewControl = null;
		private readonly Type[] _types = new Type[] { typeof(BusinessRuleSymbol), typeof(IFSymbol), typeof(THENResultSymbol), typeof(ELSEResultSymbol), typeof(ONERRORResultSymbol), typeof(ORSymbol), typeof(ANDSymbol), typeof(XORSymbol), typeof(Route) };
		#endregion

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

	}
}
